package de.szut.bomberman.game.logic;

import java.awt.Point;
import java.util.ArrayList;

import javax.swing.JLabel;

public class Move extends Thread {

	private CollisionControl collisionControlStones;
	private CollisionControl collisionControlDestroyableStones;
	private CollisionControl collisionControlBombs;

	private boolean threadPaused;
	private JLabel labelToMove;
	private Direction directionFlag;

	public Move(JLabel label, ArrayList<JLabel> stones,
			ArrayList<JLabel> destroyableStones, ArrayList<JLabel> bombs) {
		directionFlag = Direction.DEFAULT;
		threadPaused = false;
		labelToMove = label;
		collisionControlStones = new CollisionControl(stones, label);
		collisionControlDestroyableStones = new CollisionControl(
				destroyableStones, label);
		collisionControlBombs = new CollisionControl(bombs, label);

	}

	@Override
	public void run() {
		while (true) {
			if (directionFlag.equals(Direction.RIGHT)
					&& labelToMove.getLocation().getX() < 600
					&& collisionControlStones.noCollision(directionFlag)
					&& collisionControlDestroyableStones
							.noCollision(directionFlag)
					&& collisionControlBombs.noCollision(directionFlag)) {
				labelToMove.setLocation(new Double(labelToMove.getLocation()
						.getX()).intValue() + 50, new Double(labelToMove
						.getLocation().getY()).intValue());
			} else if (directionFlag.equals(Direction.LEFT)
					&& labelToMove.getLocation().getX() > 0
					&& collisionControlStones.noCollision(directionFlag)
					&& collisionControlDestroyableStones
							.noCollision(directionFlag)
					&& collisionControlBombs.noCollision(directionFlag)) {
				labelToMove.setLocation(new Double(labelToMove.getLocation()
						.getX()).intValue() - 50, new Double(labelToMove
						.getLocation().getY()).intValue());
			} else if (directionFlag.equals(Direction.UP)
					&& labelToMove.getLocation().getY() > 0
					&& collisionControlStones.noCollision(directionFlag)
					&& collisionControlDestroyableStones
							.noCollision(directionFlag)
					&& collisionControlBombs.noCollision(directionFlag)) {
				labelToMove.setLocation(new Double(labelToMove.getLocation()
						.getX()).intValue(), new Double(labelToMove
						.getLocation().getY()).intValue() - 50);
			} else if (directionFlag.equals(Direction.DOWN)
					&& labelToMove.getLocation().getY() < 600
					&& collisionControlStones.noCollision(directionFlag)
					&& collisionControlDestroyableStones
							.noCollision(directionFlag)
					&& collisionControlBombs.noCollision(directionFlag)) {
				labelToMove.setLocation(new Double(labelToMove.getLocation()
						.getX()).intValue(), new Double(labelToMove
						.getLocation().getY()).intValue() + 50);
			}

			try {
				sleep(250);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			synchronized (this) {
				while (threadPaused) {
					try {
						wait();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
	}

	public void pause() {
		threadPaused = true;

	}

	public void proceed() {
		threadPaused = false;
		notify();
	}

	public void setMovementDirection(Direction direction) {
		directionFlag = direction;
	}
}
